// pages/search/sEnterprise/sEnterprise.js

const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        // banner数据
        banCurrentSwiper: 0,
        banAutoplay:true,
        banCircular:true,
        images: [
            {
                url: app.globalData.imagesUrl + 'd-banner.jpg'
            },
            {
                url: app.globalData.imagesUrl + 'd-banner.jpg'
            },
            {
                url: app.globalData.imagesUrl + 'd-banner.jpg'
            },
            {
                url: app.globalData.imagesUrl + 'd-banner.jpg'
            }
        ],
        // 列表数据
        list1: [
            {
                url: app.globalData.imagesUrl + 'list_p1.jpg',
                title: '广告设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p2.jpg',
                title: '软件设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '数字科技'
            },
            {
                url: app.globalData.imagesUrl + 'list_p3.jpg',
                title: '房屋建设',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '房产建设'
            },
            {
                url: app.globalData.imagesUrl + 'list_p4.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p5.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p6.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            }
        ],
        list1Id: 1,
        enterprise: '',
        enterpriseDescShow: '',
        enterpriseMaxLength: 132,
        enterpriseEllipsis: false,
        enterpriseDesc:''
    },
    ellipsis: function () {
        var enterpriseEllipsis = !this.data.enterpriseEllipsis;
        var enterpriseDescShow = this.data.enterprise.introduction;
        var enterpriseMaxLength = this.data.enterpriseMaxLength;
        var enterpriseDesc = this.data.enterprise.introduction;
        enterpriseDescShow = (enterpriseDescShow.length > enterpriseMaxLength && enterpriseEllipsis) ? enterpriseDescShow.substring(0, enterpriseMaxLength - 3) + "..." : enterpriseDescShow;
        this.setData({
            enterpriseDescShow: enterpriseDescShow,
            enterpriseEllipsis: enterpriseEllipsis,
            enterpriseDesc
        },()=>{
          console.log(enterpriseDesc.length)
        })
        
    },
    // banner
    banSwiperChange: function (e) {
        this.setData({
            banCurrentSwiper: e.detail.current
        });
    },
    onLoad(options) {
      let param = {
        id: options.id
      }
      let that = this
      api.enterprise_index(param).then(function(resp){
        that.setData({
          enterprise: resp.data
        },()=>{
          that.ellipsis()
        })
        
        let parm = {
          ucode:resp.data.ucode
        }

        api.product_index(parm).then(function(res){
          let list1 = []
          for(let i = 0; i< res.data.length; i++){
            list1.push({
              id: res.data[i].id,
              url: res.data[i].url,
              title: res.data[i].title+'',
              price: res.data[i].price+'',
              companyLogoUrl: res.data[i].logo,
              companyTitle: res.data[i].companyName+'',
              is_discuss: res.data[i].is_discuss+'',
              tag: res.data[i].category+'',
              sub_title: res.data[i].sub_title
            })
          }
          that.setData({
            list1
          })
          that.update()
        })

        if(options.message_id){
          let param2 = {
            id:options.message_id
          }
          api.memberSupply1_read(param2).then(function(res){
            
          })
        }

      })

  
      //添加沟通记录
      let communicateParam = {
        ucode: app.globalData.userinfo.ucode,
        enterprise_id: options.id
      }
      api.communicate_add(communicateParam).then(function(res){

      })


    },

    // 刷新新瀑布流
    update() {
        this.data.list1Id = 1
        // 重置瀑布流组件
       // this.selectComponent('#waterfall').reset()
        let list1 = this.getMockData()
        this.setData({ list1 })
        wx.stopPullDownRefresh()
    },
    // 获取瀑布流数据
    getMockData() {
        let { list1Id } = this.data
        let list1 = []
        for (let i = 0; i < this.data.list1.length; i++) {
            list1.push({
                list1Id,
                imgUrl: app.globalData.siteUrl + this.data.list1[i].url,
                title: this.data.list1[i].title+'',
                price: this.data.list1[i].price+'',
                companyLogoUrl: app.globalData.siteUrl + this.data.list1[i].companyLogoUrl,
                companyTitle: this.data.list1[i].companyTitle+'',
                tage: this.data.list1[i].tag+'',
                is_discuss: this.data.list1[i].is_discuss+'',
                id: this.data.list1[i].id,
                sub_title: this.data.list1[i].sub_title
            })
            this.data.list1Id = ++list1Id
        }
        return list1
    },
    call_phone: function (e) {
      console.log(e.target.dataset.phone)
      wx.makePhoneCall({
        phoneNumber: e.target.dataset.phone, //这里是电话号码[假的]可以调用自己的数据this.data.xxx
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
})